/*
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-08-06     Tian Yiqing  the first version
 */

#ifndef __DRV_SPI_H__
#define __DRV_SPI_H__

#include <rthw.h>
#include <rtthread.h>
#include <board.h>

#ifdef __cplusplus
extern "C" {
#endif

struct gd32_spi_cs
{
    rcu_periph_enum cs_clk;
    uint32_t cs_port;
    uint32_t cs_pin;
};

/* gd32 spi dirver class */
struct gd32_spi
{
    uint32_t spi_periph;
    IRQn_Type irqn;
    char *bus_name;
    rcu_periph_enum spi_clk;
    rcu_periph_enum sck_clk;
    rcu_periph_enum miso_clk;
    rcu_periph_enum mosi_clk;
    
    uint32_t sck_port;
#if defined SOC_SERIES_GD32F4xx
    uint32_t sck_af;
#endif
    uint32_t sck_pin;

    uint32_t miso_port;
#if defined SOC_SERIES_GD32F4xx
    uint32_t miso_af;
#endif
    uint32_t miso_pin;

    uint32_t mosi_port;
#if defined SOC_SERIES_GD32F4xx
    uint32_t mosi_af;
#endif
    uint32_t mosi_pin;

#if defined BSP_USING_SPI_DMA
    struct dma_config *dma_tx;
    struct dma_config *dma_rx;
#endif

    struct rt_spi_message* cur_msg;
    uint32_t cur_send_result;
    uint32_t cur_recv_result;
    struct rt_completion spi_compl;
    struct rt_spi_bus spi_bus;
};

#ifdef __cplusplus
}
#endif

#endif /* __DRV_SPI_H__ */
